<template>
  <div class="scanner-container">
    <h1>病毒扫描模块</h1>
    <div class="controls">
      <button @click="startScan" :disabled="isScanning">开始扫描</button>
      <button @click="stopScan" :disabled="!isScanning">停止扫描</button>
    </div>
    <div class="progress">
      <p>扫描进度: {{ progress }}%</p>
      <progress :value="progress" max="100"></progress>
    </div>
    <div class="results">
      <h2>扫描结果</h2>
      <div v-for="(moduleResults, moduleName) in results" :key="moduleName" class="module-result">
        <h3>{{ moduleName }}</h3>
        <ul>
          <li v-for="(result, index) in moduleResults" :key="index">
            {{ result.message }} (状态: {{ result.status }})
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, onBeforeUnmount } from 'vue';
import { startScan, stopScan } from '@/api/cf/scanner';
import WebSocketService from '@/api/cf/websocket';
const isScanning = ref(false);
const progress = ref(0);
const results = ref({
  '文件扫描': [],
  '内存扫描': [],
  '注册表扫描': [],
});
const websocket = ref(null);
const startScanHandler = async () => {
  try {
    isScanning.value = true;
    progress.value = 0;
    // 清空结果
    for (const moduleName in results.value) {
      results.value[moduleName] = [];
    }

    // 调用接口启动扫描
    await startScan('/path/to/scan');

    // 初始化 WebSocket 连接
    websocket.value = new WebSocketService('ws://your-websocket-server-url');
    websocket.value.connect();

    // 监听扫描进度和结果
    websocket.value.on('message', (data) => {
      if (data.type === 'progress') {
        progress.value = data.value;
      } else if (data.type === 'result') {
        const moduleName = data.module;
        if (results.value[moduleName]) {
          results.value[moduleName].push(data);
        }
      }
    });

    websocket.value.on('close', () => {
      isScanning.value = false;
    });

  } catch (error) {
    console.error('扫描启动失败:', error);
    isScanning.value = false;
  }
};

const stopScanHandler = async () => {
  try {
    await stopScan();
    if (websocket.value) {
      websocket.value.disconnect();
    }
    isScanning.value = false;
  } catch (error) {
    console.error('扫描停止失败:', error);
  }
};

// 组件销毁时断开 WebSocket
onBeforeUnmount(() => {
  if (websocket.value) {
    websocket.value.disconnect();
  }
});
// onMounted(() => {
//     getSceneList().then(res => {
//         sceneArr.value = [{ id: 0, title: '全部' }].concat(res.data);
//         changeCate(0)
//         getList()
//     });
// });






</script>

<style lang="scss" scoped>
.scanner-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.controls {
  margin: 20px 0;
}

.progress {
  margin: 20px 0;
}

.results {
  margin-top: 20px;
}

.module-result {
  margin-bottom: 20px;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

.module-result h3 {
  margin-top: 0;
}
</style>
